import { getUserPermissions } from './services/auth';

/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState) {
  const { currentUser } = initialState || {};
  
  // 如果没有用户信息，则没有任何权限
  if (!currentUser) {
    return {
      canAdmin: false,
      hasPermission: () => false,
    };
  }

  // 获取用户权限列表
  const permissions = currentUser.permissions || [];
  
  return {
    // 是否有管理员权限
    canAdmin: currentUser?.username === 'admin' || currentUser?.username === 'superadmin',
    
    // 检查是否有指定权限
    hasPermission: (permissionIdentifier) => {
      if (!permissionIdentifier) return true;
      
      return permissions.some(
        (item) => item.identifier === permissionIdentifier.path && item.type === 'menu'
      );
    },
  };
}